package com.safe.app.service.impl;

import com.safe.app.config.context.ExecutionContext;
import com.safe.app.mapper.DepartMapper;
import com.safe.app.mapper.UserMapper;
import com.safe.app.model.Depart;
import com.safe.app.model.DepartExample;
import com.safe.app.model.User;
import com.safe.app.model.UserExample;
import com.safe.app.service.UserService;
import com.safe.app.utils.common.AESUtils;
import com.safe.app.utils.common.UUIDGenerator;
import com.safe.app.utils.constants.SystemConstant;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.lang.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.Date;
import java.util.List;

@Service
@Slf4j
public class UserServiceImpl implements UserService {


    @Autowired
    private UserMapper userMapper;
    @Autowired
    private DepartMapper departMapper;

    /**
     * 登陆
     *
     * @param loginName
     * @param passWord
     * @return
     * @throws Exception
     */
    @Override
    public User login(String loginName, String passWord) throws Exception {
        if (StringUtils.isBlank(loginName) || StringUtils.isBlank(passWord)) {
            log.error("userName or passWord can not be empty");
        }
        passWord = AESUtils.encode(passWord);
        UserExample userExample = new UserExample();
        UserExample.Criteria criteria = userExample.createCriteria();
        UserExample.Criteria criteriaOr = userExample.createCriteria();
        criteria.andUserNameEqualTo(loginName).andPasswordEqualTo(passWord);
        userExample.or(criteriaOr.andPhoneNumberEqualTo(loginName));
        List<User> users = userMapper.selectByExample(userExample);
        return users.size() > 0 ? users.get(0) : null;
    }

    /**
     * 获取用户列表
     *
     * @param pageNo
     * @param pageSize
     * @return
     */
    @Override
    public List<User> getUseList(int pageNo, int pageSize) {
        UserExample userExample = new UserExample();
        userExample.setPageNo(pageNo);
        userExample.setPageSize(pageSize);
        userExample.setOrderByClause("create_time desc");
        UserExample.Criteria criteria = userExample.createCriteria();
        criteria.andStatusEqualTo(SystemConstant.USER_ACCOUNT_STATUS_ACTIVE);
        List<User> users = userMapper.selectByExample(userExample);
        if (users.size() < 1) {
            return null;
        }
        for (User user : users) {
            user.setPassword("");
            if (StringUtils.isNotBlank(user.getDepartId())) {
                Depart depart = departMapper.selectByPrimaryKey(user.getDepartId());
                if (depart != null) {
                    user.setDepartName(depart.getDeptName());
                }
            }
        }
        return users;
    }


    /**
     * 新增用户
     * 唯一性：手机号码
     *
     * @param user
     * @return
     */
    @Override
    public boolean addUser(User user) {
        if (user == null) {
            return false;
        }
        user.setUserId(UUIDGenerator.getUUID());
        user.setCreateBy(ExecutionContext.getUserId());
        user.setCreateTime(new Date());
        int result = userMapper.insertSelective(user);
        return result > 0;
    }

    /**
     * 校验手机号码的唯一性
     *
     * @param phoneNum
     * @return
     * @throws Exception
     */
    @Override
    public boolean checkOnlyPhone(String phoneNum) throws Exception {
        UserExample userExample = new UserExample();
        UserExample.Criteria criteria = userExample.createCriteria();
        criteria.andPhoneNumberEqualTo(phoneNum);
        criteria.andStatusEqualTo(SystemConstant.USER_ACCOUNT_STATUS_ACTIVE);
        List<User> users = userMapper.selectByExample(userExample);
        return users.size() > 0;
    }

    /**
     * 部门列表
     *
     * @return
     * @throws Exception
     */
    @Override
    public List<Depart> getDepartList() throws Exception {
        final DepartExample departExample = new DepartExample();
        DepartExample.Criteria criteria = departExample.createCriteria();
        Byte delFlag = 0;
        criteria.andDelFlagEqualTo(delFlag);
        departExample.setOrderByClause("create_time desc");
        return departMapper.selectByExample(departExample);
    }

    /**
     * 新增部门
     * @param depart
     * @return
     * @throws Exception
     */
    @Override
    public boolean addDepart(Depart depart) throws Exception {
        return false;
    }
}
